package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

/**
 * 搜索 service.
 *
 * @author 宫新程
 * @since 2019/3/4 13:52
 */
@Service
@Transactional
public class ArticleService {

  @Resource private IdWorker idWorker;
  @Resource private ArticleDao articleDao;

  /** 保存. */
  public void save(Article article) {
    //    article.setId(idWorker.nextId() + "");
    articleDao.save(article);
  }

  /** 根据key搜索. */
  public Page<Article> findByKey(String key, int page, int size) {
    Pageable pageable = PageRequest.of(page - 1, size);
    return articleDao.findByTitleLikeOrContentLike("%" + key + "%", "%" + key + "%", pageable);
  }
}
